package com.crmweb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.crmweb.dto.XTreeDto;
import com.crmweb.entity.PageVo;
import com.crmweb.entity.Result;
import com.crmweb.entity.Roles;
import com.crmweb.service.ModulesService;
import com.crmweb.service.RolesService;

@RestController
public class RolesController {

	@Resource 
	private RolesService rs;
	@Resource
	private ModulesService ms;
	@RequestMapping("sys/selAllRoles")
	public Map<String, Object> selAllRoles(){
		
		List<Roles> list = rs.getAllRoles();
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("data", list);
		map.put("count", list.size());
		
		return map;
		
	}
	@RequestMapping("sys/getRolesByCondition")
	public Map<String,Object> getRolesByCondition (HttpServletRequest request){
		String page = request.getParameter("page");
		String pagesize = request.getParameter("limit");
		String name = request.getParameter("name"); 
		Roles role = new Roles();
		if(name !=null && name !="") {
			role.setName(name);
		}
		PageVo vo = new PageVo();
		vo.setPage(Integer.parseInt(page));
		vo.setPageSize(Integer.parseInt(pagesize));
		
		role.setVo(vo);
		List<Roles> list = rs.getRolesByCondition(role);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("data", list);
		map.put("count",rs.getCount());
		return map;
		
	}
	@RequestMapping("sys/checkrolesname")
	public Object checkrolesname(HttpServletRequest request) {
		String name = request.getParameter("name"); 
		
		Roles roles = rs.getRoleByName(name);
		if(roles==null) {
			Result res = new Result("","",true);
			return res;
		}else {
			Result res = new Result("","",false);
			return res;
		}
	}
	@RequestMapping("sys/saveRole")
	public Object saveRole(HttpServletRequest request) {
		String name = request.getParameter("name"); 
		int i = rs.saveRole(name);
		if(i>0) {
			Result res = new Result("添加成功","",true);
			return res;
		}else {
			Result res = new Result("添加失败","",false);
			return res;
		}
		
	}
	@RequestMapping("sys/updateRole")
	public Object updateRole(Roles roles) {
		int i = rs.updateRole(roles);
		if(i>0) {
			Result res = new Result("修改成功","",true);
			return res;
		}else {
			Result res = new Result("修改失败","",false);
			return res;
		}
		
	}
	@RequestMapping("sys/deleteRole")
	public Object deleteRole(HttpServletRequest request) {
		String rolesId = request.getParameter("id"); 
		int i = rs.deleteRole(rolesId);
		if(i>0) {
			Result res = new Result("删除成功","",true);
			return res;
		}else {
			Result res = new Result("删除失败","",false);
			return res;
		}
		
	}
	
	
	@RequestMapping("sys/getZTreeNodes")
	public Object getxtee(String id) {
		List<XTreeDto> list=rs.TirmXTreeDto(id);
	
		List<XTreeDto> nodes=rs.xTreeDtoTree(list);
		
		
		return nodes;
	}
	@RequestMapping("sys/addMdulesssByRolesId")
	public Object addMdulesByRolesId(@RequestParam("roleId")String roleId,@RequestParam("moduleId")List<Integer> moduleId) {
	    
		List<Integer>  mpids = ms.selModulesBymid(moduleId);
		for (Integer integer : mpids) {
			System.out.println("_----------------------"+integer);
			moduleId.add(integer);
		}
		
		
//		List<XTreeDto> list=rs.TirmXTreeDto(roleId);
//		
//		List<XTreeDto> nodes=rs.xTreeDtoTree(list);
//		if(nodes !=null) {
//			
//		}
		for (Integer integers : moduleId) {
			System.out.println("--------------------"+integers);
		}
		rs.delModulesByrolesId(roleId);
		
		int i= rs.addModulesByRolesId(roleId, moduleId);
	     if(i>0) {
				Result r=new Result("添加模块成功","",true);
				return r;
			}else {
				Result r=new Result("添加模块失败","",false);
				return r;
			}
		
		
		
		
	}
 }
